package app.beit.fintrack.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.fintrack.bean.HospitalBean;
import app.beit.fintrack.bean.ProvinceBean;
import app.beit.fintrack.bean.ZoneBean;
import app.beit.fintrack.service.MasterService;
import app.beit.fintrack.utils.constr.MsgConstr;

@Controller
@RequestMapping("/master")
public class MasterController extends BaseController {
	
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private MasterService masterService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value="getZone", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<List<ZoneBean>> getZone(final HttpServletRequest request){
		this.log.info("get zones");
		ResponseFintrack<List<ZoneBean>> response = null;
		try{
			final List<ZoneBean> beans = this.masterService.getZone();
			if(beans != null && beans.size() > 0){
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="getHospital", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<List<HospitalBean>> getHospital(final HttpServletRequest request, final @RequestParam("zoneId") String zoneId, 
			final @RequestParam("provinceId") String provinceId){
		this.log.info("get hospitals");
		ResponseFintrack<List<HospitalBean>> response = null;
		try{
			final List<HospitalBean> beans = this.masterService.getHospitalByZoneIdAndProvinceId(zoneId, provinceId);
			if(beans != null && beans.size() > 0){
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getAllHospital", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<List<HospitalBean>> getAllHospital(final HttpServletRequest request){
		this.log.info("get all hospitals");
		ResponseFintrack<List<HospitalBean>> response = null;
		try{
			final List<HospitalBean> beans = this.masterService.getAllHospital();
			if(beans != null && beans.size() > 0){
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="getProvince", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<List<ProvinceBean>> getProvince(final HttpServletRequest request, final @RequestParam("zoneId") String zoneId){
		this.log.info("get provinces");
		ResponseFintrack<List<ProvinceBean>> response = null;
		try{
			final List<ProvinceBean> beans = this.masterService.getProvinceByZoneId(zoneId);
			if(beans != null && beans.size() > 0){
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getProvinceByHospitalCode", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<ProvinceBean> getProvinceFromHCode(final HttpServletRequest request, final @RequestParam("hospitalCode") String hospitalCode){
		this.log.info("get provinces By Hospital code");
		ResponseFintrack<ProvinceBean> response = null;
		try{
			final ProvinceBean bean = this.masterService.getProvinceByHCode(hospitalCode);
			if(bean != null){
				response = super.responseSuccess();
				response.setDatas(bean);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getZoneByHospitalCode", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<ZoneBean> getZoneFromHCode(final HttpServletRequest request, final @RequestParam("hospitalCode") String hospitalCode){
		this.log.info("get zone By Hospital code");
		ResponseFintrack<ZoneBean> response = null;
		try{
			final ZoneBean bean = this.masterService.getZoneByHCode(hospitalCode);
			if(bean != null){
				response = super.responseSuccess();
				response.setDatas(bean);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getZoneFromDomainName", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<ZoneBean> getZoneFromDomainName(final HttpServletRequest request, final @RequestParam("domainName") String domainName){
		
		ResponseFintrack<ZoneBean> response = null;
		try{
			final ZoneBean bean = new ZoneBean();
			this.log.info("domainName is "+domainName);
			String zoneId = "";
			
			switch (domainName) {
			case "cmi.nhso.go.th":
				zoneId = "01";
				break;
			case "plk.nhso.go.th":
				zoneId = "02";
				break;
			case "nkw.nhso.go.th":
				zoneId = "03";
				break;
			case "srr.nhso.go.th":
				zoneId = "04";
				break;
			case "rbr.nhso.go.th":
				zoneId = "05";
				break;
			case "ryg.nhso.go.th":
				zoneId = "06";
				break;
			case "kkn.nhso.go.th":
				zoneId = "07";
				break;
			case "udt.nhso.go.th":
				zoneId = "08";
				break;
			case "nkm.nhso.go.th":
				zoneId = "09";
				break;
			case "ubn.nhso.go.th":
				zoneId = "10";
				break;
			case "srn.nhso.go.th":
				zoneId = "11";
				break;
			case "skl.nhso.go.th":
				zoneId = "12";
				break;
			case "bk.nhso.go.th":
				zoneId = "13";
				break;
			default:
				zoneId = "00";
				break;
			}
			
			bean.setZoneId(zoneId);
			if(bean.getZoneId() != null && !bean.getZoneId().equals("")){
				response = super.responseSuccess();
				response.setDatas(bean);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		return response;
	}
}
